package com.tac.actions;

import com.tac.util.TimeSleepUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
//南京博物院预约
public class NanjinbwActions {

    static int i=1;
    public void bw(ChromeDriver driver) throws InterruptedException, ParseException {

        //点击个人预约
        driver.findElementByCssSelector(".wrap>.main-content>section>div>div.container>div:nth-child(1)>dl:nth-child(2)>dt:nth-child(1)>a").click();
        Thread.sleep(20000);//扫码

        String time="2024-04-24 18:00:00";
        TimeSleepUtil.tsleep(time);

        Thread.sleep(500);
        //切换到当前浏览器
        Set<String> windowHandles = driver.getWindowHandles();
        for(String windowHandle:windowHandles){
            if(windowHandle!=driver.getWindowHandle()){
                driver.switchTo().window(windowHandle);
            }
        }
        //刷新页面
        driver.navigate().refresh();
        //切换iframe
        WebElement webElement = driver.findElementByCssSelector(".iframe-box");
        driver.switchTo().frame(webElement);
        //选中日期
        driver.findElementByCssSelector(".layui-laydate-content>table>tbody:nth-child(2)>tr:nth-child(5)>td:nth-child(4)").click();
        //.layui-laydate-content>table>tbody:nth-child(2)>tr:nth-child(5)>td:nth-child(4)

        while(i==1){
            try {
                //判断能不能取到复选框元素，要是取不到会出现异常，然后重新取一次
                WebElement elementByCssSelector = driver.findElementByCssSelector(".visitor-box>form>div:nth-child(10)>table:nth-child(3)>tbody>tr:nth-child(2)>td>input");
                 i=0;//0表示正常
                System.out.println("复选框存在");
            }catch (Exception e){
                 i=1;//1表示复选框不存在
                System.out.println("复选框不存在");
                Thread.sleep(500);
                driver.navigate().refresh();
                //切换iframe    刷新完后好像要重新切换iframe
                Thread.sleep(150);
                webElement = driver.findElementByCssSelector(".iframe-box");
                driver.switchTo().frame(webElement);
                //选中日期
                driver.findElementByCssSelector(".layui-laydate-content>table>tbody:nth-child(2)>tr:nth-child(4)>td:nth-child(5)").click();
            }
        }
        //选中两个复选框
        driver.findElementByCssSelector(".visitor-box>form>div:nth-child(10)>table:nth-child(3)>tbody>tr:nth-child(2)>td>input").click();
        driver.findElementByCssSelector(".visitor-box>form>div:nth-child(10)>table:nth-child(3)>tbody>tr:nth-child(3)>td>input").click();
        //点击确认提交
        driver.findElementByCssSelector(".visitor-box>form>div:nth-child(11)>input").click();
        Thread.sleep(20000);//这里是没有下一步就快点手动点击

    }
}
